package com.yiwei.hdb.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.hdb.R;
import com.yiwei.hdb.activity.Application;
import com.yiwei.hdb.activity.HelpActivity;
import com.yiwei.hdb.activity.HelpCenterActivity;
import com.yiwei.hdb.activity.MainActivityNew;
import com.yiwei.hdb.activity.V3BankMoreListActivity;
import com.yiwei.hdb.activity.V3IdcardIdentityActivity;
import com.yiwei.hdb.activity.V3MyBorrowRecordActivity;
import com.yiwei.hdb.activity.V3MyFundActivity;
import com.yiwei.hdb.activity.V3MySettingActivity;
import com.yiwei.hdb.activity.V4HelpCenterActivity;
import com.yiwei.hdb.activity.WechatChatActivity;
import com.yiwei.hdb.api.callback.UserContract;
import com.yiwei.hdb.api.requestInfo.UserRequest;
import com.yiwei.hdb.constant.UserGlobal;
import com.yiwei.hdb.model.UserModel;
import com.yiwei.hdb.util.PreferenceUtils;
import com.yiwei.hdb.util.Spans;
import com.yiwei.hdb.util.ToastUtil;
import com.yiwei.hdb.util.Util;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by xsooy-pc on 18-10-31.
 */

public class FragmentMyV4 extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, UserContract.View {
    @BindView(R.id.img_logo)
    ImageView imgLogo;
    @BindView(R.id.btn_setting)
    ImageView btnSetting;
    @BindView(R.id.btn_wechat)
    ImageView btnWechat;
    @BindView(R.id.img_icon)
    CircleImageView imgIcon;
    @BindView(R.id.txt_name)
    TextView txtName;
    @BindView(R.id.txt_money)
    TextView txtMoney;
    @BindView(R.id.btn_my_zhangdan)
    LinearLayout btnMyZhangdan;
    @BindView(R.id.btn_my_fund)
    LinearLayout btnMyFund;
    @BindView(R.id.btn_my_bankcard)
    LinearLayout btnMyBankcard;
    @BindView(R.id.btn_my_help)
    LinearLayout btnMyHelp;
    @BindView(R.id.txt_version)
    TextView txtVersion;
    @BindView(R.id.btn_my_version)
    LinearLayout btnMyVersion;
    Unbinder unbinder;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.txt_edu_total)
    TextView txtEduTotal;
    @BindView(R.id.txt_edu_can_use)
    TextView txtEduCanUse;
    private UserRequest api;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_my_v4;
    }

    @Override
    protected void initView() {
        txtVersion.setText("v" + Util.getVersion(getContext()));
        api = UserRequest.create();
        api.attachView(this);
//        EventBus.getDefault().register(this);
        refresh.setOnRefreshListener(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        api.detachView();
        api = null;
        unbinder.unbind();
    }

    @Override
    public void onResume() {
        super.onResume();
        api.getUserInfo();
    }

    @OnClick({R.id.btn_setting, R.id.btn_wechat, R.id.btn_my_zhangdan, R.id.btn_my_fund, R.id.btn_my_bankcard, R.id.btn_my_help, R.id.btn_my_version})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_setting:
                if (Util.checkLogin(getActivity())) {
                    Util.startActivity(getActivity(), V3MySettingActivity.class);
                }
                break;
            case R.id.btn_wechat:
                Util.startActivity(getActivity(), WechatChatActivity.class);
                break;
            case R.id.btn_my_zhangdan:
                if (Util.checkLogin(getActivity()))
                    Util.startActivity(getActivity(), V3MyBorrowRecordActivity.class);
                break;
            case R.id.btn_my_fund:
                if (Util.checkLogin(getActivity()))
                    Util.startActivity(getActivity(), V3MyFundActivity.class);
                break;
            case R.id.btn_my_bankcard:
                if (Util.checkLogin(getActivity())) {
                    if (!UserGlobal.getIsIdcardPass()) {
                        ToastUtil.makeText(getContext(), "请先完成实名");
                        Util.startActivity(getContext(), V3IdcardIdentityActivity.class);
                        return;
                    }
                    Util.startActivity(getActivity(), V3BankMoreListActivity.class);
                }
                break;
            case R.id.btn_my_help:
                Util.startActivity(getActivity(), V4HelpCenterActivity.class);
                break;
            case R.id.btn_my_version:
                String versionName = Util.getVersion(getContext());
                versionName = versionName.replace("", "");
                String version = PreferenceUtils.getString(getContext(), "lastest_version", "1.0.0");
                String versionNew = version.replace("", "");
                if (((MainActivityNew) getActivity()).compareVersion(versionNew, versionName) > 0) {
                    ((MainActivityNew) getActivity()).checkUpdate();
                } else {
                    ToastUtil.makeText(getContext(), "已经是最新版本");
                }
                break;
        }
    }

    @Override
    public void onRefresh() {
        api.getUserInfo();
    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {
        if (refresh != null && refresh.isRefreshing()) {
            refresh.setRefreshing(false);
        }
    }

    @Override
    public void onRequestStart() {
        refresh.setRefreshing(true);
    }

    @Override
    public void getUserInfoSuccess(UserModel model) {
        if (model.getStatus() == 0) {
            ToastUtil.makeText(getContext(), "请先登录");
        } else if (model.getStatus() == 98) {
            ((MainActivityNew) getActivity()).gotoUpdate();
        } else if (model.getStatus() == 1) {
            setUserData(model);
        }
    }

    private void setUserData(UserModel model) {
        UserGlobal.saveUserInfo(model);
        txtName.setText(model.getReal_name());

        if (model.getIdcardpassed().equals("0")) {
            String mobile = model.getMobile();
            if (Util.isNumeric(mobile) && mobile.length() == 11)
                txtName.setText(Util.replaceString(mobile, 5, 4, '*'));
        } else {
            txtName.setText(model.getReal_name());
        }
        Spanned total_edu = Spans.with(this).font("总额度 ", getResources().getDimensionPixelSize(R.dimen.x30), 0xff49506D).
                font(model.money_amount.replace(".00", ""), getResources().getDimensionPixelSize(R.dimen.x36), 0xff00C3BE).
                build();
        txtEduTotal.setText(total_edu);

        Spanned can_use_edu = Spans.with(this).font("可用额度 ", getResources().getDimensionPixelSize(R.dimen.x30), 0xff49506D).
                font(model.getMoney().replace(".00", ""), getResources().getDimensionPixelSize(R.dimen.x36), 0xff00C3BE).
                build();
        txtEduCanUse.setText(can_use_edu);

        txtMoney.setText("账户余额 " + model.getUser_balance() + "元");
    }
}
